<?php
// TODO: pom    澳买拼团 逻辑处理
namespace Admin\Common;

class Logic
{

    // TODO: 获取 商品 详情要显示的数据
    public function getDetailDataForPieces($PiecesId)
    {
      $ret = array('123');


      return $ret;
    }

  // TODO: 获取列表要显示的数据
  public function check_login($data)
  {
     $user_name = trim($data['user_name']);
     $admin_list = D('Admin')->field('id,name,pwd,power')->where('name = "'.$user_name.'"')->find();

     if(!empty($admin_list)){
         if(sha1(trim($data['pwd'])) == $admin_list['pwd']){

           unset($admin_list['pwd']);

           $_SESSION['admin_user'] = $admin_list;
           //保存权限
           if($admin_list['power']=="all"){
             return $admin_list['id'];exit;
           }
           $where['id'] = array('in','"'.$admin_list['power'].'"');
           $rulesArr = array();
           $admin_rules_list = D('Menu')->field('name_url')->where($where)->select();

           foreach ($admin_rules_list as $key => $value) {
               array_push($rulesArr,$value['name_url']);
           }
           if(!empty($rulesArr)){
               $_SESSION['admin_rules'] = implode(',',$rulesArr);
           }
           return $admin_list['id'];
         }
     }
  }




    // TODO: 获取列表要显示的数据
    public function getPiecesData()
    {
      // LibTools::fileDebug(,"");

      $ret = array();




      return $ret;
    }


 function get_str($id,$dataArray = array()) {
     $list = D('menu')->field('id,name as text,iconCls,name_url as url,parent_id')->where('parent_id='.$id)->order('sort desc')->select();
    if($list){
           foreach ($list as $key2 => $value2) {
                      $list[$key2]['children'] =array();
                     $value2['children'] = $this->get_str($value2['id'],$dataArr);
                      $list[$key2]= $value2;
                     $dataArr = $list;
                      // $this->get_str($value2['id'],$dataArr);
                    }

           }
       return $dataArr;


}

    // TODO: 获取父集信息
    public function parent_nav($id)
    {
      $list = D('menu')->where('id ='.$id)->find();
      if(!empty($list)){
      $parent =D('menu')->where('id = '.$list['parent_id'])->find();
      return $parent;
     }
}
    // TODO: 获取当前信息
    public function one_nav($id)
    {
      $list = D('menu')->where('id ='.$id)->find();
      if(!empty($list)){
      return $list;
     }
      
    }



    // TODO: 获取菜单树
    public function menu()
    {

      //获取到该用户的权限
      $user_power = $_SESSION['admin_user']['power'];
      if($user_power == 'all'){
           //获取到菜单
            $menu_list = D('menu')->field('id,name as text,iconCls,name_url as url,parent_id')->where('parent_id=0 and is_menu=1')->order('sort desc')->select();
            foreach ($menu_list as $key => $value) {
                    $value['children']= $this->get_str($value['id']);
                    $menu_list[$key] = $value;

            }
      }else{
        $ids_str =$user_power;
        $where['parent_id'] =0;
        $where['id'] = array('in',$ids_str);
                 $menu_list = D('menu')->field('id,name as text,iconCls,name_url as url,parent_id')->where($where)->order('sort desc')->select();
                 
                  foreach ($menu_list as $key => $value) {
                          $value['children']= $this->get_str2($value['id'],$ids_str);
                          // if(empty($value['children'])){
                          //    unset($menu_list[$key]);
                          // }else{
                            $menu_list[$key] = $value;
                          // }
                          
                  }


      }
      

      return $menu_list;
    }


// TODO: 添加子菜单
    public function add_menu($data,$id){
      $res = array('success'=>0,'msg'=>'菜单添加失败');
      $data['name'] = $data['name'];
      $data['url_name'] = $data['url_name'];
      $data['sort'] = $data['sort'];
       $data['iconCls'] = "";
       $data['closed'] =1;
       $data['add_time'] = date('Y-m-d H:i:s');
       $data['parent_id'] = $id;
       $res = D('menu')->add($data);
       if($res){
        $res = array('success'=>1,'msg'=>'菜单添加成功');
       }
      return $res;
    }

// TODO: 获取用户权限

 public function sel_nav($id){
  $nav = D('Nav')->where('u_id='.$id)->field('m_id')->select();
  $ids = array();
  foreach ($nav as $key => $value) {
       $ids[$key] = $value['m_id'];
  }
  $ids_str = implode(',',$ids);
  $where['parent_id'] =0;
  $where['id'] = array('in',$ids_str);
           $menu_list = D('menu')->field('id,name as text,iconCls,name_url as url,parent_id')->where($where)->order('sort desc')->select();
            foreach ($menu_list as $key => $value) {
                    $value['children']= $this->get_str2($value['id'],$ids_str);
                    $menu_list[$key] = $value;
            }
 return $menu_list;
 }


  function get_str2($id,$ids_str,$dataArray = array()) {
     $where['parent_id'] =$id;
     $where['id'] = array('in',"$ids_str");
     $where['is_menu'] = 1;
     $list = D('menu')->field('id,name as text,iconCls,name_url as url,parent_id')->where($where)->order('sort desc')->select();

    if($list){
           foreach ($list as $key2 => $value2) {
                      $list[$key2]['children'] =array();

                     $value2['children'] = $this->get_str2($value2['id'],$ids_str,$dataArr);
                      $list[$key2]= $value2;
                     $dataArr = $list;
                      // $this->get_str($value2['id'],$dataArr);
                    }

           }
           //print_r($list);
       return $dataArr;


}

// TODO: 获取用户权限

 public function sel_nav_add($id){
  $nav = D('Nav')->where('u_id='.$id)->field('m_id')->select();
  $ids = array();
  foreach ($nav as $key => $value) {
       $ids[$key] = $value['m_id'];
  }
  $ids_str = implode(',',$ids);
  $where['parent_id'] =0;
  // $where['id'] = array('in',$ids_str);
           $menu_list = D('menu')->field('id,name as text,iconCls,name_url as url,parent_id')->where($where)->order('sort desc')->select();
            foreach ($menu_list as $key => $value) {
                  if(in_array($value['id'],$ids)){
                      $value['checked'] = true;
                  }
                    $value['children']= $this->get_str3($value['id'],$ids_str);
                    $menu_list[$key] = $value;
            }
 return $menu_list;
 }



 function get_str3($id,$ids_str,$dataArray = array()) {
    $where['parent_id'] =$id;
    $ids = explode(',',$ids_str);
    // $where['id'] = array('in',"$ids_str");
    $list = D('menu')->field('id,name as text,iconCls,name_url as url,parent_id')->where($where)->order('sort desc')->select();

   if($list){
          foreach ($list as $key2 => $value2) {
                     $list[$key2]['children'] =array();
                     if(in_array($value2['id'],$ids)){
                       $value2['checked'] = true;
                     }
                    $value2['children'] = $this->get_str3($value2['id'],$ids_str,$dataArr);
                     $list[$key2]= $value2;
                    $dataArr = $list;
                     // $this->get_str($value2['id'],$dataArr);
                   }

          }
          //print_r($list);
      return $dataArr;


}
}








?>
